<?php

namespace addons\groupon\library\traits\model\order;

use addons\groupon\library\Wechat;
use addons\groupon\model\Order;
use addons\groupon\model\OrderItem;
use addons\groupon\model\Store;
use think\Cache;

trait OrderScope
{
    // 已失效
    public function scopeInvalid($query)
    {
        return $query->where('status', Order::STATUS_INVALID);
    }

    // 已取消
    public function scopeCancel($query)
    {
        return $query->where('status', Order::STATUS_CANCEL);
    }

    // 未支付
    public function scopeNopay($query)
    {
        return $query->where('status', Order::STATUS_NOPAY);
    }

    // 未备货
    public function scopeNosend($query, $onlyNosend = true)
    {
        // 未备货，或者已备货但是还未到自提点的
        $dispatchStatus = ['in', [OrderItem::DISPATCH_STATUS_NOSEND, OrderItem::DISPATCH_STATUS_READY]];
        if ($onlyNosend) {
            // 只查未备货的
            $dispatchStatus = ['in', [OrderItem::DISPATCH_STATUS_NOSEND]];
        }
        $where = [
            'dispatch_status' => $dispatchStatus,
            'refund_status' => ['not in', [OrderItem::REFUND_STATUS_OK, OrderItem::REFUND_STATUS_FINISH]]       // 没有退款完成
        ];

        return $query->whereExists(function ($query) use ($where) {
            $order_table_name = (new Order())->getQuery()->getTable();
            $table_name = (new OrderItem())->getQuery()->getTable();
            $query->table($table_name)->where('order_id=' . $order_table_name . '.id')->where($where);
        });
    }


    // 已备货，待自提点签收
    public function scopeNoarrive($query)
    {
        $where = [
            'dispatch_status' => OrderItem::DISPATCH_STATUS_READY,
            'refund_status' => ['not in', [OrderItem::REFUND_STATUS_OK, OrderItem::REFUND_STATUS_FINISH]]       // 没有退款完成
        ];

        return $query->whereExists(function ($query) use ($where) {
            $order_table_name = (new Order())->getQuery()->getTable();
            $table_name = (new OrderItem())->getQuery()->getTable();
            $query->table($table_name)->where('order_id=' . $order_table_name . '.id')->where($where);
        });
    }

    // 待提货
    public function scopeNoget($query, $onlyget = false)
    {
        // 已备货，和已到货
        $dispatchStatus = [OrderItem::DISPATCH_STATUS_READY, OrderItem::DISPATCH_STATUS_ARRIVE];
        if ($onlyget) {
            // 只查已到货，不查自提点代签收的
            $dispatchStatus = [OrderItem::DISPATCH_STATUS_ARRIVE];
        }
        $where = [
            'dispatch_status' => ['in', $dispatchStatus],
            'refund_status' => ['not in', [OrderItem::REFUND_STATUS_OK, OrderItem::REFUND_STATUS_FINISH]]       // 没有退款完成
        ];

        return $query->whereExists(function ($query) use ($where) {
            $order_table_name = (new Order())->getQuery()->getTable();
            $table_name = (new OrderItem())->getQuery()->getTable();
            $query->table($table_name)->where('order_id=' . $order_table_name . '.id')->where($where);
        });
    }


    /**
     * 包含待备货，和已备货带提货的所有
     *
     * @return void
     */
    public function scopeSends($query)
    {
        $dispatchStatus = [
            OrderItem::DISPATCH_STATUS_NOSEND,      // 待备货
            OrderItem::DISPATCH_STATUS_READY,       // 已备货，待到货
            OrderItem::DISPATCH_STATUS_ARRIVE       // 已备货，已到货自提点
        ];

        $where = [
            'dispatch_status' => ['in', $dispatchStatus],
            'refund_status' => ['not in', [OrderItem::REFUND_STATUS_OK, OrderItem::REFUND_STATUS_FINISH]]       // 没有退款完成
        ];

        return $query->whereExists(function ($query) use ($where) {
            $order_table_name = (new Order())->getQuery()->getTable();
            $table_name = (new OrderItem())->getQuery()->getTable();
            $query->table($table_name)->where('order_id=' . $order_table_name . '.id')->where($where);
        });
    }



    // 待评价
    public function scopeNocomment($query)
    {
        $where = [
            'dispatch_status' => OrderItem::DISPATCH_STATUS_GETED,
            'refund_status' => ['not in', [OrderItem::REFUND_STATUS_OK, OrderItem::REFUND_STATUS_FINISH]],       // 没有退款完成
            'comment_status' => OrderItem::COMMENT_STATUS_NO
        ];

        return $query->whereExists(function ($query) use ($where) {
            $order_table_name = (new Order())->getQuery()->getTable();
            $table_name = (new OrderItem())->getQuery()->getTable();
            $query->table($table_name)->where('order_id=' . $order_table_name . '.id')->where($where);
        });
    }


    public function scopeRefundStatus($query)
    {
        $where = [
            'refund_status' => ['<>', OrderItem::REFUND_STATUS_NOREFUND],       // 只要申请过退款
        ];

        return $query->whereExists(function ($query) use ($where) {
            $order_table_name = (new Order())->getQuery()->getTable();
            $table_name = (new OrderItem())->getQuery()->getTable();
            $query->table($table_name)->where('order_id=' . $order_table_name . '.id')->where($where);
        });
    }


    // 已支付
    public function scopePayed($query)
    {
        return $query->where('status', 'in', [Order::STATUS_PAYED, Order::STATUS_FINISH]);
    }

    // 已完成
    public function scopeFinish($query)
    {
        return $query->where('status', Order::STATUS_FINISH);
    }

    public function scopeCanDelete($query)
    {
        return $query->where('status', 'in', [
            Order::STATUS_CANCEL,
            Order::STATUS_INVALID,
            Order::STATUS_FINISH
        ]);
    }



    /**
     * 已完成，这个只手机门店订单查询
     */
    public function scopeStoreFinish($query)
    {
        // 收完货就算已完成
        $where = [
            'dispatch_status' => OrderItem::DISPATCH_STATUS_GETED,
            'refund_status' => ['not in', [OrderItem::REFUND_STATUS_OK, OrderItem::REFUND_STATUS_FINISH]]       // 没有退款完成
        ];

        return $query->where(function ($query) use ($where) {
            $query->where('status', Order::STATUS_FINISH)
                ->whereExists(function ($query) use ($where) {
                    $order_table_name = (new Order())->getQuery()->getTable();
                    $table_name = (new OrderItem())->getQuery()->getTable();
                    $query->table($table_name)->where('order_id=' . $order_table_name . '.id')->where($where);
                });
        });
        
    }
}
